#!/usr/bin/python
# -*- coding: iso-8859-1 -*-

import Tkinter
import os

class client_console_tk(Tkinter.Tk):
	def __init__(self,parent):
		Tkinter.Tk.__init__(self,parent)
		self.parent=parent
		self.initialize()

	def initialize(self):
		self.grid()
		

		#Label del Dominio		
		self.labelVariable = Tkinter.StringVar()		
		self.labelVariable.set("Dominio")		
		dlabel=Tkinter.Label(self,textvariable=self.labelVariable,anchor="w",fg="white",bg="blue")
        	dlabel.grid(column=0,row=0,columnspan=2,sticky='EW') 		
		
		#Label del ip servidor 		
		self.labelVariable = Tkinter.StringVar()		
		self.labelVariable.set("Ip del servidor")		
		ilabel=Tkinter.Label(self,textvariable=self.labelVariable,anchor="w",fg="white",bg="blue")
        	ilabel.grid(column=0,row=1,columnspan=2,sticky='EW') 

		#Label del ip cliente 		
		self.labelVariable = Tkinter.StringVar()		
		self.labelVariable.set("Ip cliente")		
		olabel=Tkinter.Label                        (self,textvariable=self.labelVariable,anchor="w",fg="white",bg="blue")
        	olabel.grid(column=0,row=2,columnspan=2,sticky='EW')
                
                #Label de la interfas del cliente		
		self.labelVariable = Tkinter.StringVar()		
		self.labelVariable.set("Interfaces cliente")		
		alabel=Tkinter.Label(self,textvariable=self.labelVariable,anchor="w",fg="white",bg="blue")
        	alabel.grid(column=0,row=3,columnspan=2,sticky='EW')
        
		#Textfield del parametro Dominio		
		self.domValue = Tkinter.StringVar()		
		self.entry = Tkinter.Entry(self,textvariable=self.domValue)
        	self.entry.grid(column=1,row=0, sticky='EW')
		self.domValue.set(u"1")
		
		#Textfield del ip servidor
		self.ipValue=Tkinter.StringVar()		
		self.entry = Tkinter.Entry(self,textvariable=self.ipValue)
        	self.entry.grid(column=1,row=1, sticky='EW')
		self.ipValue.set(u"0")		
               
               #Textfield del ip cliente 		
		self.Valueclient = Tkinter.StringVar()		
		self.entry = Tkinter.Entry(self,textvariable=self.Valueclient)
        	self.entry.grid(column=1,row=2, sticky='EW')
		self.Valueclient.set(u"1")
		
		#Textfield del interfas del cliente
		self.valueinterface=Tkinter.StringVar()		
		self.entry = Tkinter.Entry(self,textvariable=self.valueinterface)
        	self.entry.grid(column=1,row=3, sticky='EW')
		self.valueinterface.set(u"0")	
		
		
	
		#Boton ejecutar
		run=Tkinter.Button(self,text=u"Ejecutar",command=self.Start_ButtonClick)
	        run.grid(column=0,row=4, sticky="W")
		
		#Boton detener
		stop=Tkinter.Button(self,text=u"Detener",command=self.Stop_ButtonClick)
	        stop.grid(column=1,row=4, sticky="E")
		
	def Start_ButtonClick(self):

				
		if(os.system('g++ Client_packer.cpp -o cp')):
			pass
		elif(os.system('g++ Client_unpacker.cpp -o cu')):
			pass
                elif(os.system('rm client.txt')):
			pass
		elif(os.system('g++ Quimera_protocol_client.cpp  -o qpc')):
 			pass
                elif(os.system('g++ proto.cpp -o proto')):
 			pass
		elif(os.system('tcpdump -i '+self.valueinterface.get()+' -n -l \'tcp[tcpflags]&17==17 or tcp[tcpflags]&18==18 or tcp[tcpflags]&4==4 or tcp[tcpflags]&24==24\' and src host '+self.Valueclient.get()+' >> client.txt &')):
 			pass

                elif(os.system('./cp > /dev/tty1 &')):
			pass
		elif(os.system('./cu > /dev/tty2 &')):
			pass
                elif(os.system('./qpc 2 '+self.domValue.get()+' '+self.ipValue.get()+' > /dev/tty3 &')):
                        pass	       
                
                elif(os.system('./proto 1 '+self.domValue.get()+'> /dev/tty4 &')):
                        pass	   
			
	
			
		

	def Stop_ButtonClick(self):
		self.L=[]		
		self.L.append(os.popen('pidof cp').readline())
		self.L.append(os.popen('pidof cu').readline())
		self.L.append(os.popen('pidof qpc').readline())
		self.L.append(os.popen('pidof proto').readline())		
		for item in self.L:
			print item			
			os.system('kill '+item)

	

if __name__=="__main__":
	app=client_console_tk(None)
	app.title('Consola del servidor Quimera')
	app.mainloop()


